package com.example.mediamoudle.scrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import org.jetbrains.annotations.NotNull;

public class MyNestedScrollView  extends HorizontalScrollView {
    private MyNestedScrollViewScrollChange scrollChange;
    public MyNestedScrollView(@NonNull @NotNull Context context) {
       this(context,null);
    }

    public MyNestedScrollView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
       this(context,attrs,0);
        mGestureDetector = new GestureDetector(new YScrollDetector());
        setFadingEdgeLength(0);

    }

    public MyNestedScrollView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mGestureDetector = new GestureDetector(new YScrollDetector());
        setFadingEdgeLength(0);

    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollChange!=null){
            scrollChange.onScroll(l,t,oldl,oldt);
        }
    }
    public void setScrollChange(MyNestedScrollViewScrollChange scrollChange){
        this.scrollChange = scrollChange;
    }

    public interface  MyNestedScrollViewScrollChange{
        void onScroll(int left, int top, int oldLeft, int oldTop);

    }
    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

}
